//
//  XLChannelItem.m
//  XLChannelControlDemo
//
//  Created by MengXianLiang on 2017/3/3.
//  Copyright © 2017年 MengXianLiang. All rights reserved.
//

#import "XLChannelItem.h"

@interface XLChannelItem (){
    UILabel *_textLabel;
    UIButton * _deleteButton;
    CAShapeLayer *_borderLayer;
}
@end

@implementation XLChannelItem

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

-(void)initUI{
    self.userInteractionEnabled = true;
//    self.clipsToBounds = YES;
    self.layer.cornerRadius = 5.0f;
    self.backgroundColor = [UIColor whiteColor];
    self.isEditing = NO;
    
    _textLabel = [UILabel new];
    _textLabel.frame = self.bounds;
    _textLabel.textAlignment = NSTextAlignmentCenter;
    _textLabel.textColor = [UIColor xm_colorFromRGB:0x5D646E];
    _textLabel.font = [UIFont xm_font:15.0];
    _textLabel.adjustsFontSizeToFitWidth = true;
    _textLabel.userInteractionEnabled = true;
    [self addSubview:_textLabel];
    
    _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_deleteButton setImage:[UIImage imageNamed:@"image_delete"] forState:UIControlStateNormal];
    [_deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
    _deleteButton.hidden = YES;
    [self addSubview:_deleteButton];
//    [self addBorderLayer];
}

-(void)deleteButtonClick{
    NSLog(@"deleteButtonClick");
}
-(void)addBorderLayer{
    _borderLayer = [CAShapeLayer layer];
    _borderLayer.bounds = self.bounds;
    _borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    _borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:_borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    _borderLayer.lineWidth = 1;
    _borderLayer.lineDashPattern = @[@5, @3];
    _borderLayer.fillColor = [UIColor clearColor].CGColor;
    _borderLayer.strokeColor = [self backgroundColor].CGColor;
    [self.layer addSublayer:_borderLayer];
    _borderLayer.hidden = true;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _textLabel.frame = self.bounds;
    _deleteButton.frame = CGRectMake(self.bounds.size.width - 10, -10, 20, 20);
}

#pragma mark -
#pragma mark 配置方法

-(UIColor*)backgroundColor{
    return [UIColor colorWithRed:241/255.0f green:241/255.0f blue:241/255.0f alpha:1];
}

-(UIColor*)textColor{
    return [UIColor colorWithRed:40/255.0f green:40/255.0f blue:40/255.0f alpha:1];
}

-(UIColor*)lightTextColor{
    return [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];
}

#pragma mark -
#pragma mark Setter
-(void)setIsEditing:(BOOL)isEditing{
    _isEditing = isEditing;
    _deleteButton.hidden = !_isEditing;
}
-(void)setTitle:(NSString *)title{
    _title = title;
    _textLabel.text = title;
}

-(void)setIsMoving:(BOOL)isMoving{
    _isMoving = isMoving;
    if (_isMoving) {
        self.backgroundColor = [UIColor clearColor];
        _borderLayer.hidden = false;
    }else{
        self.backgroundColor = [self backgroundColor];
        _borderLayer.hidden = true;
    }
}

-(void)setIsFixed:(BOOL)isFixed{
    _isFixed = isFixed;
    if (isFixed) {
//        _textLabel.textColor = [UIColor xm_MainColor];
        _textLabel.backgroundColor = [UIColor xm_colorFromRGB:0xFFFFF];
    }else{
//        _textLabel.textColor = [UIColor redColor];
        _textLabel.backgroundColor = [UIColor xm_colorFromRGB:0xEDEEF0];
    }
}



-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    // 当前控件上的点转换到chatView上
    CGPoint chatP = [self convertPoint:point toView:_deleteButton];
    // 判断下点在不在chatView上
    if ([_deleteButton pointInside:chatP withEvent:event]) {
        return _deleteButton;
    }else{
        return [super hitTest:point withEvent:event];
    }
}

@end
